package com.hc.fdkforwarders.dao;


import com.hc.fdkforwarders.common.utils.StringUtils;
import com.hc.fdkforwarders.vo.*;
import org.jooq.Record1;
import org.jooq.Result;
import org.jooq.SelectConditionStep;
import org.jooq.impl.DSL;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

import static com.hc.fdkforwarders.db.tables.PlatformSku.PLATFORM_SKU;
import static com.hc.fdkforwarders.db.tables.PlatformSpu.PLATFORM_SPU;
import static com.hc.fdkforwarders.db.tables.SellerFetchOrder.SELLER_FETCH_ORDER;

@Repository
public class AllSkuListDao extends BaseDao{
    //代拿接口3
    public AllSkuListVO_3 queryAllSkuList(GoodsNumCoListQueryVO param){
        AllSkuListVO_3 allSkuListVO_3 = new AllSkuListVO_3();
        SelectConditionStep<?> query = db.select(SELLER_FETCH_ORDER.BIZ_NAME.as("bizFullName"),
                SELLER_FETCH_ORDER.BIZ_ID.as("bizId"),
                DSL.countDistinct(SELLER_FETCH_ORDER.PLATFORM_SKU_ID).as("totalKind"),
                DSL.count(SELLER_FETCH_ORDER.PLATFORM_SKU_ID).as("totalNum"))
                .from(SELLER_FETCH_ORDER)
                .where(SELLER_FETCH_ORDER.CITY_ID.eq(param.getCityId()));
        if(param.getMarketId()!=null){
            query = query.and(SELLER_FETCH_ORDER.MARKET_ID.eq(param.getMarketId()));
        }
        if(param.getFloorId()!=null){
            query = query.and(SELLER_FETCH_ORDER.FLOOR_CODE.eq(param.getFloorId()));
        }
        if(!StringUtils.isEmpty(param.getBizFullName())){
            query = query.and(SELLER_FETCH_ORDER.BIZ_NAME.eq(param.getBizFullName()));
        }
        query = query.and(SELLER_FETCH_ORDER.STATUS.eq(new Byte("1")));

        List<AllSkuListVO> allSkuListVOS = query.groupBy(SELLER_FETCH_ORDER.BIZ_NAME).fetchInto(AllSkuListVO.class);

        Long total = Long.valueOf(allSkuListVOS.size());

        for (AllSkuListVO allSpu : allSkuListVOS) {
            List<Long> spuIdList = new ArrayList<>();
            spuIdList =  db.select(SELLER_FETCH_ORDER.PLATFORM_SPU_ID)
                    .from(SELLER_FETCH_ORDER)
                    .where(SELLER_FETCH_ORDER.BIZ_ID.eq(allSpu.getBizId()))
                    .fetchInto(Long.class);

            List<SpuListVO> spuListVOS = db.select(PLATFORM_SPU.MAIN_IMG_URL
                    ,PLATFORM_SPU.SPU_GOODS_NO
                    ,PLATFORM_SPU.ID.as("spuId")
                    ,PLATFORM_SPU.SPU_NAME
            )
                    .from(PLATFORM_SPU)
                    .where(PLATFORM_SPU.ID.in(spuIdList)).fetchInto(SpuListVO.class);

            for (SpuListVO spu :spuListVOS) {
                spu.setSkuList(db.select(PLATFORM_SKU.ID.as("skuId")
                        ,PLATFORM_SKU.SKU_NAME)
                        .from(PLATFORM_SPU)
                        .innerJoin(PLATFORM_SKU)
                        .on(PLATFORM_SKU.SPU_ID.eq(PLATFORM_SPU.ID))
                        .where(PLATFORM_SKU.SPU_ID.eq(spu.getSpuId())).fetchInto(SkuListVO.class));
            }
            allSpu.setSpuList(spuListVOS);
        }
        allSkuListVO_3.setAllSkuListVO(allSkuListVOS);
        allSkuListVO_3.setTotal(total);
        return allSkuListVO_3;
    }
}
